package com.channel.temperature.collect.app.util;

import android.os.Build;


import androidx.annotation.RequiresApi;

import com.channel.temperature.collect.data.response.OfflineBean;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class ExcelUtil {
    private final static String UTF8_ENCODING = "UTF-8";

    /**
     * 单元格的格式设置 字体大小 颜色 对齐方式、背景颜色等...
     */
    @RequiresApi(api = Build.VERSION_CODES.TIRAMISU)
    public static boolean writeToExcel(String fileName, String[] colName, String companyName,
                                       String deviceName, String taskName, String taskDec,
                                       String startTime, String endTime, String delayTime,
                                       String bzMax, String bzMin, String bzMaxTime, String bzMinTime,
                                       String hjMax, String hjMin, String hjMaxTime, String hjMinTime,
                                       String ylMax, String ylMin, String ylMaxTime, String ylMinTime, ArrayList<OfflineBean> objList) {

        try {
            File file = new File(fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
//            FileWriter csvWriter = new FileWriter(file, Charset.forName("GBK"), true);
            var fos = new FileOutputStream(file);
            var osr = new OutputStreamWriter(fos, "GBK");
            BufferedWriter bufferedWriter = new BufferedWriter(osr);
            bufferedWriter.write("测量单位," + companyName + "\n");

            bufferedWriter.newLine();
            bufferedWriter.write("测量信息" + "\n");
            bufferedWriter.write("设备名称:," + deviceName + "\n");
            bufferedWriter.write("任务名称:," + taskName + "\n");
            bufferedWriter.write("任务备注:," + taskDec + "\n");
            bufferedWriter.write("开始时间:," + startTime + "\n");
            bufferedWriter.write("结束时间:," + endTime + "\n");
            bufferedWriter.write("最小间隔(S):," + delayTime + "\n");
            bufferedWriter.newLine();
            bufferedWriter.write("统计信息" + "\n");
            bufferedWriter.write("板载温度最大值(℃)," + bzMax + ",时间," + bzMaxTime + "\n");
            bufferedWriter.write("板载温度最小值(℃)," + bzMin + ",时间," + bzMinTime + "\n");
            bufferedWriter.write("环境温度最大值(℃)," + hjMax + ",时间," + hjMaxTime + "\n");
            bufferedWriter.write("环境温度最小值(℃)," + hjMin + ",时间," + hjMinTime + "\n");
            bufferedWriter.write("环境压力最大值(MPa)," + ylMax + ",时间," + ylMaxTime + "\n");
            bufferedWriter.write("环境压力最小值(MPa)," + ylMin + ",时间," + ylMinTime + "\n");
            bufferedWriter.newLine();
            bufferedWriter.write("测量数据" + "\n");
            bufferedWriter.write("时间,板载温度(℃),环境温度(℃),环境压力(MPa)" + "\n");


            for (int j = 0; j < objList.size(); j++) {

                OfflineBean projectBean = (OfflineBean) objList.get(j);
                List<String> list = new ArrayList<>();
                String time = projectBean.getTime();
                String[] temp = time.split("&");
                long resultTime = Long.parseLong(temp[0]);
                list.add(MyUtil.DateToStr(new Date(resultTime * 1000)));
                list.add("" + MyUtil.formatFloat(
                        projectBean.getGX2(),
                        2,
                        0.00f
                ));
                list.add("" + MyUtil.formatFloat(
                        projectBean.getPT2(),
                        3,
                        0.000f
                ));
                list.add("" + MyUtil.formatFloat(
                        projectBean.getPressure2(),
                        3,
                        0.000f
                ));
                bufferedWriter.write(MyUtil.DateToStr(new Date(resultTime * 1000)) + "," + MyUtil.formatFloat(
                        projectBean.getGX2(),
                        2,
                        0.00f
                ) + "," + MyUtil.formatFloat(
                        projectBean.getPT2(),
                        3,
                        0.000f
                ) + "," + MyUtil.formatFloat(
                        projectBean.getPressure2(),
                        3,
                        0.000f
                ) + "\n");
            }
            // 关闭文件写入流
            bufferedWriter.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}